package com.syni.mdd.yhd.common.all.controller.api.coupon;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dto.reponseDto.groupBuy.SearchCodeRpDto;
import com.syni.mdd.yhd.common.all.dto.reponseDto.groupBuy.searchCode.Code;
import com.syni.mdd.yhd.common.all.dto.reponseDto.verRecord.VerRecordRpDto;
import com.syni.mdd.yhd.common.all.dto.requestDto.groupBuy.GroupBuyUseReDto;
import com.syni.mdd.yhd.common.all.entity.*;
import com.syni.mdd.yhd.common.all.entity.coupon.BmsUserReceCoupon;
import com.syni.mdd.yhd.common.all.service.*;
import com.syni.mdd.yhd.common.all.vo.rp.userBusiness.GetVerRecordDetailsRpVo;
import com.syni.mdd.sjb.common.component.utils.common.utils.*;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiCoupon.*;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddAll.CouponRestService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @className OrderRestApi
 * @description TOO
 * @Author cfx
 * @DATE 2020/3/18 10:06
 * @VERSION 1.0
 **/
@RestController
@RequestMapping("/apiCoupon")
@Log4j2
public class CouponRestApi implements CouponRestService {

    @Autowired
    BmsDxOrderService bmsDxOrderService;

    @Autowired
    BmsDxGroupBuyUseService bmsDxGroupBuyUseService;

    @Autowired
    BmsUserReceCouponService bmsUserReceCouponService;

    @Autowired
    BmsBusinessService bmsBusinessService;

    @Autowired
    BmsUserBusinessService bmsUserBusinessService;

    @Autowired
    BmsVerRecordService bmsVerRecordService;

    @Autowired
    BmsDxGroupBuyService bmsDxGroupBuyService;

    public final static int SEACH_WAY_BY_ORDERID = 1;
    public final static int SEACH_WAY_BY_COUPON = 2;
    public final static int SEACH_WAY_BY_CODE = 3;
    //兼容
    public final static int SEARCH_WAY_BY_NEW_WAY = 4;



    @PostMapping("/search")
    public RpcResponseVo search(@RequestBody SearchDto searchDto){
        Date nowTime = new Date();
        Integer searchWay = searchDto.getSearchWay();
        if(searchWay == SEACH_WAY_BY_ORDERID){
            BmsDxOrder bmsDxOrder = bmsDxOrderService.getBaseDao().findByIdAndIsDelete(searchDto.getOrderId(), Constants.COMMON_FLAG_FALSE_INT);
            if(bmsDxOrder == null){
                return RpcResponseVo.result(ResultCode.CODE_ERROR_05, ResultMsg.CODE_ERROR_05);
            }
            //查询下核销id
            if(StringUtils.isBlank(searchDto.getVerId())) {
                List<BmsVerRecord> bmsVerRecords = bmsVerRecordService.getRecodeByOrderId(bmsDxOrder.getId());
                if (CollectionUtils.isNotEmpty(bmsVerRecords)) {
                    searchDto.setVerId(bmsVerRecords.get(0).getVerId());
                }
            }
            BmsDxGroupBuy bmsDxGroupBuy = bmsDxOrder.getBmsDxGroupBuy();
            if(bmsDxGroupBuy != null){
                SearchDto.BmsGroupBuyData bmsGroupBuyData = BeanUtils.copyProperties(bmsDxGroupBuy, SearchDto.BmsGroupBuyData.class);
                bmsGroupBuyData.setGroupBuyId(bmsDxGroupBuy.getId());
                bmsGroupBuyData.setExpireTime(DateTimeUtils.converDateToLocalDateTime(bmsDxGroupBuy.getExpireTime()));
                bmsGroupBuyData.setGroupBuyName(bmsDxGroupBuy.getGroupName());
                try {
                    if(StringUtils.isNotBlank(bmsDxGroupBuy.getGroupPhotosSmall())){
                        bmsGroupBuyData.setGroupPhotos(JSONObject.parseArray(bmsDxGroupBuy.getGroupPhotosSmall(),String.class));
                    }
                    if(CollectionUtils.isEmpty(bmsGroupBuyData.getGroupPhotos())){
                        if(StringUtils.isNotBlank(bmsDxGroupBuy.getGroupPhotos())){
                            bmsGroupBuyData.setGroupPhotos(JSONObject.parseArray(bmsDxGroupBuy.getGroupPhotos(),String.class));
                        }
                    }
                    if(CollectionUtils.isEmpty(bmsGroupBuyData.getGroupPhotos())){
                        if(StringUtils.isNotBlank(bmsDxGroupBuy.getGroupImg())){
                            bmsGroupBuyData.getGroupPhotos().add(bmsDxGroupBuy.getGroupImg());
                        }
                    }
                }catch (Exception e){
                    log.error(e.getMessage(),e);
                }
                searchDto.setBmsGroupBuy(bmsGroupBuyData);
            }
            List<BmsDxGroupBuyUse> buyUses = bmsDxGroupBuyUseService.getGroupByCodeOrderNoUse(bmsDxOrder.getId());
            if(CollectionUtils.isNotEmpty(buyUses)){
                buyUses.forEach(by -> {
                    SearchDto.Code code = new SearchDto.Code();
                    code.setStatus(by.getStatus());
                    code.setCodeNumber(by.getGroupBuyCode());
                    if (code.getStatus() == Constants.GROUP_REFUND_COMPLETE) {
                        code.setStatus(Constants.GROUP_REFUND_COMPLETE_FLECT);
                    }
                    if (code.getStatus() == Constants.GROUP_NO_USE && bmsDxGroupBuy.getExpireTime() != null) {
                        if (DateTimeUtils.match(nowTime,DateTimeUtils.addDay(bmsDxGroupBuy.getExpireTime(),1),DateTimeUtils.MATCH_MODEL_GTE)){
                            code.setStatus(Constants.IS_OVER_EXPIRE);
                        }
                    }
                    searchDto.getCodes().add(code);
                });
            }
            searchDto.setCodeType(Constants.VER_TYPE_MUY);
        }else if(searchWay == SEACH_WAY_BY_COUPON){
            Optional<BmsUserReceCoupon> bmsUserReceCouponOptional = bmsUserReceCouponService.getBaseDao().findById(searchDto.getCouponId());
            if(!bmsUserReceCouponOptional.isPresent()){
                return RpcResponseVo.result(ResultCode.CODE_ERROR_05, ResultMsg.CODE_ERROR_05);
            }
            BmsUserReceCoupon bmsUserReceCoupon = bmsUserReceCouponOptional.get();
            BmsBusinessCoupon bmsBusinessCoupon = bmsUserReceCoupon.getBmsBusinessCoupon();
            //查询下核销id,内调的情况可能不用查询
            if(StringUtils.isBlank(searchDto.getVerId())) {
                List<BmsVerRecord> bmsVerRecords = bmsVerRecordService.getRecodeByReceId(bmsUserReceCoupon.getId());
                if (CollectionUtils.isNotEmpty(bmsVerRecords)) {
                    searchDto.setVerId(bmsVerRecords.get(0).getVerId());
                }
            }
            SearchDto.Code code = new SearchDto.Code();
            code.setStatus(bmsUserReceCoupon.getStatus());
            code.setCodeNumber(bmsUserReceCoupon.getConsumerCode());
            if (code.getStatus() == Constants.GROUP_NO_USE && bmsBusinessCoupon.getSendStartTime() != null) {
                if (DateTimeUtils.match(nowTime,bmsBusinessCoupon.getSendStartTime(),DateTimeUtils.MATCH_MODEL_LTE)){
                    code.setStatus(Constants.GROUP_REFUND_NO_CAN_USE);
                }
            }
            searchDto.getCodes().add(code);
            if(bmsBusinessCoupon != null){
                SearchDto.UserReceCoupon userReceCoupon = new SearchDto.UserReceCoupon();
                userReceCoupon.setCouponName(bmsBusinessCoupon.getCouponName());
                userReceCoupon.setCouponId(bmsUserReceCoupon.getId());
                userReceCoupon.setExpireTime(DateTimeUtils.converDateToLocalDateTime(bmsUserReceCoupon.getExpireTime()));
                userReceCoupon.setCouponType(bmsBusinessCoupon.getCouponType());
                BmsBusiness bmsBusiness = bmsBusinessService.getBusinessById(bmsUserReceCoupon.getBusinessId());
                if(bmsBusiness != null) {
                    userReceCoupon.setBusinessName(bmsBusiness.getVendorName());
                }
                searchDto.setUserReceCoupon(userReceCoupon);
            }
            searchDto.setCodeType(Constants.VER_TYPE_RECE);
        }else if(searchWay == SEACH_WAY_BY_CODE){
            ParamsMap paramsMap = new ParamsMap();
            paramsMap.put("code",searchDto.getCode());
            Map<String,Object> retMap = bmsUserBusinessService.searchCode(paramsMap);
            if(!ResultMapHelper.isSuccess(retMap)){
                return RpcResponseVo.result(ResultCode.CODE_ERROR_05, ResultMsg.CODE_ERROR_05);
            }

            Map<String,Object> userData = (Map<String, Object>) retMap.get("userData");
            Integer haveData =Integer.parseInt(userData.get("havingData").toString());
            if(haveData == Constants.COMMON_FLAG_FALSE_INT){
                return RpcResponseVo.result(ResultCode.CODE_ERROR_05, ResultMsg.CODE_ERROR_05);
            }
            Integer codeType = Integer.parseInt(userData.get("codeType").toString());
            SearchCodeRpDto searchCodeRpDto = (SearchCodeRpDto) retMap.get("data");
            if(codeType == Constants.VER_TYPE_MUY){
                BmsDxGroupBuy bmsDxGroupBuy = searchCodeRpDto.getBmsDxGroupBuy();
                if(bmsDxGroupBuy != null){
                    SearchDto.BmsGroupBuyData bmsGroupBuyData = BeanUtils.copyProperties(bmsDxGroupBuy, SearchDto.BmsGroupBuyData.class);
                    bmsGroupBuyData.setGroupBuyName(bmsDxGroupBuy.getGroupName());
                    bmsGroupBuyData.setGroupBuyId(bmsDxGroupBuy.getId());
                    bmsGroupBuyData.setExpireTime(DateTimeUtils.converDateToLocalDateTime(bmsDxGroupBuy.getExpireTime()));
                    try {
                        if(StringUtils.isNotBlank(bmsDxGroupBuy.getGroupPhotosSmall())){
                            bmsGroupBuyData.setGroupPhotos(JSONObject.parseArray(bmsDxGroupBuy.getGroupPhotosSmall(),String.class));
                        }
                        if(CollectionUtils.isEmpty(bmsGroupBuyData.getGroupPhotos())){
                            if(StringUtils.isNotBlank(bmsDxGroupBuy.getGroupPhotos())){
                                bmsGroupBuyData.setGroupPhotos(JSONObject.parseArray(bmsDxGroupBuy.getGroupPhotos(),String.class));
                            }
                        }
                        if(CollectionUtils.isEmpty(bmsGroupBuyData.getGroupPhotos())){
                            if(StringUtils.isNotBlank(bmsDxGroupBuy.getGroupImg())){
                                bmsGroupBuyData.getGroupPhotos().add(bmsDxGroupBuy.getGroupImg());
                            }
                        }
                    }catch (Exception e){
                        log.error(e.getMessage(),e);
                    }
                    searchDto.setBmsGroupBuy(bmsGroupBuyData);
                }
                List<Code> buyUses = searchCodeRpDto.getCodes();
                if(CollectionUtils.isNotEmpty(buyUses)){
                    buyUses.forEach(by -> {
                        SearchDto.Code code = new SearchDto.Code();
                        code.setStatus(by.getStatus());
                        code.setCodeNumber(by.getCodeNumber());
                        if (code.getStatus() == Constants.GROUP_REFUND_COMPLETE) {
                            code.setStatus(Constants.GROUP_REFUND_COMPLETE_FLECT);
                        }
                        if (code.getStatus() == Constants.GROUP_NO_USE && bmsDxGroupBuy.getExpireTime() != null) {
                            if (DateTimeUtils.match(nowTime,DateTimeUtils.addDay(bmsDxGroupBuy.getExpireTime(),1),DateTimeUtils.MATCH_MODEL_GTE)){
                                code.setStatus(Constants.IS_OVER_EXPIRE);
                            }
                        }
                        searchDto.getCodes().add(code);
                    });
                }
                searchDto.setCodeType(Constants.VER_TYPE_MUY);
            }else if(codeType == Constants.VER_TYPE_RECE){
                BmsUserReceCoupon bmsUserReceCoupon = searchCodeRpDto.getBmsUserReceCoupon();
                if(bmsUserReceCoupon == null){
                    return RpcResponseVo.result(ResultCode.CODE_ERROR_05, ResultMsg.CODE_ERROR_05);
                }
                BmsBusinessCoupon bmsBusinessCoupon = bmsUserReceCoupon.getBmsBusinessCoupon();
                SearchDto.Code code = new SearchDto.Code();
                code.setStatus(bmsUserReceCoupon.getStatus());
                code.setCodeNumber(bmsUserReceCoupon.getConsumerCode());
                if (code.getStatus() == Constants.GROUP_NO_USE && bmsBusinessCoupon.getSendStartTime() != null) {
                    if (DateTimeUtils.match(nowTime,bmsBusinessCoupon.getSendStartTime(),DateTimeUtils.MATCH_MODEL_LTE)){
                        code.setStatus(Constants.GROUP_REFUND_NO_CAN_USE);
                    }
                }
                searchDto.getCodes().add(code);
                if(bmsBusinessCoupon != null){
                    SearchDto.UserReceCoupon userReceCoupon = new SearchDto.UserReceCoupon();
                    userReceCoupon.setCouponName(bmsBusinessCoupon.getCouponName());
                    userReceCoupon.setCouponId(bmsUserReceCoupon.getId());
                    userReceCoupon.setExpireTime(DateTimeUtils.converDateToLocalDateTime(bmsUserReceCoupon.getExpireTime()));
                    userReceCoupon.setCouponType(bmsBusinessCoupon.getCouponType());
                    BmsBusiness bmsBusiness = bmsBusinessService.getBusinessById(bmsUserReceCoupon.getBusinessId());
                    if(bmsBusiness != null) {
                        userReceCoupon.setBusinessName(bmsBusiness.getVendorName());
                    }
                    searchDto.setUserReceCoupon(userReceCoupon);
                }
                searchDto.setCodeType(Constants.VER_TYPE_RECE);
            }
            //查询下核销id
            List<BmsVerRecord> bmsVerRecords = bmsVerRecordService.getRecodeByCode(searchDto.getCode());
            if(CollectionUtils.isNotEmpty(bmsVerRecords)){
                searchDto.setVerId(bmsVerRecords.get(0).getVerId());
            }
        }
        return RpcResponseVo.success(searchDto,ResultMsg.MSG_GET_SUCCESS);
    }

    @PostMapping("/searchByVerId")
    public RpcResponseVo searchByVerId(@RequestBody SearchDto searchDto){
        BmsVerRecord bmsVerRecord = bmsVerRecordService.getRecodeByVerId(searchDto.getVerId());
        if(bmsVerRecord == null){
            return RpcResponseVo.result(ResultCode.CODE_ERROR_05,ResultMsg.MSG_COUPON_NO_EXIST);
        }
        if(bmsVerRecord.getVerType() == Constants.VER_TYPE_MUY){
            searchDto.setOrderId(bmsVerRecord.getOrderId());
            searchDto.setSearchWay(SEACH_WAY_BY_ORDERID);
            //检查下是否是当前商家的码
            if(!bmsDxGroupBuyUseService.checkCouponBelongBusiness(searchDto.getBusinessUserId(),bmsVerRecord.getOrderId())){
                return RpcResponseVo.result(ResultCode.CODE_ERROR_05,ResultMsg.MSG_COUPON_NO_EXIST);
            }
            //内调
        }else if(bmsVerRecord.getVerType() == Constants.VER_TYPE_RECE){
            if(!bmsUserReceCouponService.checkCouponBelongBusiness(searchDto.getBusinessUserId(),bmsVerRecord.getReceId())){
                return RpcResponseVo.result(ResultCode.CODE_ERROR_05,ResultMsg.MSG_COUPON_NO_EXIST);
            }
            searchDto.setSearchWay(SEACH_WAY_BY_COUPON);
            searchDto.setCouponId(bmsVerRecord.getReceId());
        }
        return this.search(searchDto);
    }

    @PostMapping("/searchByVerNo")
    public RpcResponseVo searchByVerNo(@RequestBody SearchDto searchDto){
        List<BmsVerRecord> bmsVerRecords = bmsVerRecordService.getRecodeByCode(searchDto.getCode());
        if(CollectionUtils.isEmpty(bmsVerRecords)){
            return RpcResponseVo.result(ResultCode.CODE_ERROR_05,ResultMsg.MSG_COUPON_NO_EXIST);
        }
        BmsVerRecord bmsVerRecord = bmsVerRecords.get(0);
        if(bmsVerRecord.getVerType() == Constants.VER_TYPE_MUY){
            searchDto.setOrderId(bmsVerRecord.getOrderId());
            searchDto.setSearchWay(SEACH_WAY_BY_ORDERID);
            //检查下是否是当前商家的码
            if(!bmsDxGroupBuyUseService.checkCouponBelongBusiness(searchDto.getBusinessId(),bmsVerRecord.getOrderId())){
                return RpcResponseVo.result(ResultCode.CODE_ERROR_05,ResultMsg.MSG_COUPON_NO_EXIST);
            }
            //内调
        }else if(bmsVerRecord.getVerType() == Constants.VER_TYPE_RECE){
            if(!bmsUserReceCouponService.checkCouponBelongBusiness(searchDto.getBusinessId(),bmsVerRecord.getReceId())){
                return RpcResponseVo.result(ResultCode.CODE_ERROR_05,ResultMsg.MSG_COUPON_NO_EXIST);
            }
            searchDto.setSearchWay(SEACH_WAY_BY_COUPON);
            searchDto.setCouponId(bmsVerRecord.getReceId());
        }
        return this.search(searchDto);
    }

    @Override
    @PostMapping("/searchCouponDetail")
    public RpcResponseVo searchCouponDetail(@RequestBody SearchCouponDetailDto searchCouponDetailDto) {
        String verId = searchCouponDetailDto.getVerId();
        BmsVerRecord bmsVerRecord = bmsVerRecordService.getRecodeByVerId(verId);
        if(bmsVerRecord == null){
            return RpcResponseVo.result(ResultCode.CODE_ERROR_05,ResultMsg.CODE_ERROR_05);
        }
        if(bmsVerRecord.getVerType() == Constants.VER_TYPE_MUY){
            searchCouponDetailDto.setCodeType(Constants.VER_TYPE_MUY);
            Optional<BmsDxOrder> bmsDxOrder = bmsDxOrderService.getBaseDao().findById(bmsVerRecord.getOrderId());
            if(bmsDxOrder.isPresent()){
               BmsDxGroupBuy bmsDxGroupBuy = bmsDxOrder.get().getBmsDxGroupBuy();
               if(bmsDxGroupBuy != null){
                   bmsDxGroupBuyService.postHandleBusiness(bmsDxGroupBuy);
                   bmsDxGroupBuyService.postHandleGroupContent(bmsDxGroupBuy);
                   bmsDxGroupBuyService.postHandleShowTemplateId(bmsDxGroupBuy);
                   SearchCouponDetailDto.BmsGroupBuyDetailData bmsGroupBuyDetailData = BeanUtils.copyProperties(bmsDxGroupBuy, SearchCouponDetailDto.BmsGroupBuyDetailData.class);
                   searchCouponDetailDto.setBuyDetailData(bmsGroupBuyDetailData);
                   try {
                       if(StringUtils.isNotBlank(bmsDxGroupBuy.getGroupPhotosSmall())){
                           bmsGroupBuyDetailData.setGroupPhotos(JSONObject.parseArray(bmsDxGroupBuy.getGroupPhotosSmall(),String.class));
                       }
                       if(CollectionUtils.isEmpty(bmsGroupBuyDetailData.getGroupPhotos())){
                           if(StringUtils.isNotBlank(bmsDxGroupBuy.getGroupPhotos())){
                               bmsGroupBuyDetailData.setGroupPhotos(JSONObject.parseArray(bmsDxGroupBuy.getGroupPhotos(),String.class));
                           }
                       }
                       if(CollectionUtils.isEmpty(bmsGroupBuyDetailData.getGroupPhotos())){
                           if(StringUtils.isNotBlank(bmsDxGroupBuy.getGroupImg())){
                               bmsGroupBuyDetailData.getGroupPhotos().add(bmsDxGroupBuy.getGroupImg());
                           }
                       }
                   }catch (Exception e){
                       log.error(e.getMessage(),e);
                   }
                   try {
                       if(StringUtils.isNotBlank(bmsDxGroupBuy.getGroupDetails())){
                           bmsGroupBuyDetailData.setGroupDetails(JSONObject.parseArray(bmsDxGroupBuy.getGroupDetails(),String.class));
                       }
                       if(CollectionUtils.isEmpty(bmsGroupBuyDetailData.getGroupDetails())){
                           if(StringUtils.isNotBlank(bmsDxGroupBuy.getDetailImg())){
                               bmsGroupBuyDetailData.getGroupDetails().add(bmsDxGroupBuy.getDetailImg());
                           }
                       }
                   }catch (Exception e){
                       log.error(e.getMessage(),e);
                   }
                   if(bmsDxGroupBuy.getBmsBusiness() != null) {
                       SearchCouponDetailDto.BusinessData businessData = new SearchCouponDetailDto.BusinessData();
                       businessData.setVendorName(bmsDxGroupBuy.getBmsBusiness().getVendorName());
                       businessData.setLogoUrl(bmsDxGroupBuy.getBmsBusiness().getLogoUrl());
                       bmsGroupBuyDetailData.setBusinessData(businessData);
                   }
                   if(StringUtils.isNotBlank(bmsDxGroupBuy.getGroupContent())) {
                       List<SearchCouponDetailDto.GroupContent> groupContent = BeanUtils.copyList(bmsDxGroupBuy.getGroupContentRpDto(), SearchCouponDetailDto.GroupContent.class);
                       bmsGroupBuyDetailData.setGroupContent(groupContent);
                   }
               }
            }
        }else if(bmsVerRecord.getVerType() == Constants.VER_TYPE_RECE){
            searchCouponDetailDto.setCodeType(Constants.VER_TYPE_RECE);
            Optional<BmsUserReceCoupon> bmsUserReceCoupon = bmsUserReceCouponService.getBaseDao().findById(bmsVerRecord.getReceId());
            if(bmsUserReceCoupon.isPresent()){
                BmsUserReceCoupon bu = bmsUserReceCoupon.get();
                BmsBusinessCoupon bmsBusinessCoupon = bu.getBmsBusinessCoupon();
                SearchCouponDetailDto.UserReceDetailData userReceDetailData = new SearchCouponDetailDto.UserReceDetailData();
                userReceDetailData.setCouponName(bmsBusinessCoupon.getCouponName());
                userReceDetailData.setExpireTime(DateTimeUtils.converDateToLocalDateTime(bu.getExpireTime()));
                BmsBusiness bmsBusiness = bmsBusinessService.getBusinessById(bmsBusinessCoupon.getBusinessId());
                if(bmsBusiness != null){
                    SearchCouponDetailDto.BusinessData businessData = new SearchCouponDetailDto.BusinessData();
                    businessData.setVendorName(bmsBusiness.getVendorName());
                    businessData.setLogoUrl(bmsBusiness.getLogoUrl());
                    userReceDetailData.setBusiness(businessData);
                }
                userReceDetailData.setCouponType(bmsBusinessCoupon.getCouponType());
                if(StringUtils.isNotBlank(bmsBusinessCoupon.getCouponContent())){
                    JSONObject content = JSON.parseObject(bmsBusinessCoupon.getCouponContent());
                    if(content.containsKey("desc")){
                        userReceDetailData.setCouponDesc(content.getString("desc"));
                    }
                }
                searchCouponDetailDto.setUserReceDetailData(userReceDetailData);
            }
        }
        return RpcResponseVo.success(searchCouponDetailDto,ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    @PostMapping("/searchVerCouponDetail")
    public RpcResponseVo searchVerCouponDetail(@RequestBody SearchVerCouponDetailDto searchVerCouponDetailDto) {
        //内调方法
        ParamsMap paramsMap = new ParamsMap();
        paramsMap.put("verRecordId",searchVerCouponDetailDto.getVerRecordId());
        paramsMap.put("businessId",searchVerCouponDetailDto.getBusinessId());
        Map<String,Object> retMap =  bmsUserBusinessService.getVerRecordDetails(paramsMap);
        if(!ResultMapHelper.isSuccess(retMap)){
            return RpcResponseVo.result(ResultCode.CODE_ERROR_05, ResultMsg.CODE_ERROR_05);
        }
        GetVerRecordDetailsRpVo getVerRecordDetailsRpVo = (GetVerRecordDetailsRpVo) retMap.get("data");
        //组装团购信息
        searchVerCouponDetailDto.setCodeType(getVerRecordDetailsRpVo.getType());
        if(getVerRecordDetailsRpVo.getType() == Constants.VER_TYPE_MUY){
            SearchVerCouponDetailDto.BmsGroupBuyDetailData bmsGroupBuyDetailData = new SearchVerCouponDetailDto.BmsGroupBuyDetailData();
            searchVerCouponDetailDto.setBuyDetailData(bmsGroupBuyDetailData);
            bmsGroupBuyDetailData.setTextContent(getVerRecordDetailsRpVo.getRichText());
            bmsGroupBuyDetailData.setExpireTime(getVerRecordDetailsRpVo.getExpireTime());
            bmsGroupBuyDetailData.setReceivedDesc(getVerRecordDetailsRpVo.getReceivedDesc());
            BmsBusiness bmsBusiness = getVerRecordDetailsRpVo.getBmsBusiness();
            if(bmsBusiness != null) {
                bmsGroupBuyDetailData.setBusiness(BeanUtils.copyProperties(bmsBusiness,SearchVerCouponDetailDto.BusinessData.class));
            }
            BmsDxGroupBuy bmsDxGroupBuy = getVerRecordDetailsRpVo.getBmsDxGroupBuy();
            if(bmsDxGroupBuy != null){
                bmsDxGroupBuyService.postHandleShowTemplateId(bmsDxGroupBuy);
                bmsDxGroupBuyService.postHandleGroupDetailImg(bmsDxGroupBuy);
                bmsGroupBuyDetailData.setShowTemplateId(bmsDxGroupBuy.getShowTemplateId());
                List<SearchVerCouponDetailDto.GroupContent> groupContent = BeanUtils.copyList(bmsDxGroupBuy.getGroupContentRpDto(), SearchVerCouponDetailDto.GroupContent.class);
                bmsGroupBuyDetailData.setGroupContent(groupContent);
                bmsGroupBuyDetailData.setGroupDetails(bmsDxGroupBuy.getGroupDetailArray());

                bmsGroupBuyDetailData.setGroupName(bmsDxGroupBuy.getGroupName());
                if(StringUtils.isNotBlank(bmsDxGroupBuy.getGroupPhotosSmall())){
                    bmsGroupBuyDetailData.setGroupImg(JSONObject.parseArray(bmsDxGroupBuy.getGroupPhotosSmall(),String.class).get(0));
                }
                if(StringUtils.isEmpty(bmsGroupBuyDetailData.getGroupImg())){
                    if(StringUtils.isNotBlank(bmsDxGroupBuy.getGroupPhotos())){
                        bmsGroupBuyDetailData.setGroupImg(JSONObject.parseArray(bmsDxGroupBuy.getGroupPhotos(),String.class).get(0));
                    }
                }
                if(StringUtils.isEmpty(bmsGroupBuyDetailData.getGroupImg())){
                    if(StringUtils.isNotBlank(bmsDxGroupBuy.getGroupImg())){
                        bmsGroupBuyDetailData.setGroupImg(bmsDxGroupBuy.getGroupImg());
                    }
                }
            }
            SearchVerCouponDetailDto.BmsDxOrderData bmsDxOrderData = new SearchVerCouponDetailDto.BmsDxOrderData();
            searchVerCouponDetailDto.setBmsDxOrders(bmsDxOrderData);
            bmsDxOrderData.setOrderNo(getVerRecordDetailsRpVo.getOrderNo());
            bmsDxOrderData.setDiscountMoney(getVerRecordDetailsRpVo.getDiscountMoney());
            bmsDxOrderData.setShopMoney(getVerRecordDetailsRpVo.getShopMoney());
            bmsDxOrderData.setUserPay(getVerRecordDetailsRpVo.getUserPay());
            bmsDxOrderData.setUserName(getVerRecordDetailsRpVo.getUserName());
        }else if(getVerRecordDetailsRpVo.getType() == Constants.VER_TYPE_RECE) {
            SearchVerCouponDetailDto.UserReceDetailData userReceDetailData = new SearchVerCouponDetailDto.UserReceDetailData();
            searchVerCouponDetailDto.setUserReceDetailData(userReceDetailData);
            userReceDetailData.setExpireTime(getVerRecordDetailsRpVo.getExpireTime());
            BmsBusiness bmsBusiness = getVerRecordDetailsRpVo.getBmsBusiness();
            if (bmsBusiness != null) {
                userReceDetailData.setBusiness(BeanUtils.copyProperties(bmsBusiness, SearchVerCouponDetailDto.BusinessData.class));
            }
            BmsBusinessCoupon bmsBusinessCoupon = getVerRecordDetailsRpVo.getBmsBusinessCoupon();
            if (bmsBusinessCoupon != null) {
                if(StringUtils.isNotBlank(bmsBusinessCoupon.getCouponContent())){
                    JSONObject content = JSON.parseObject(bmsBusinessCoupon.getCouponContent());
                    if(content.containsKey("desc")){
                        userReceDetailData.setCouponDesc(content.getString("desc"));
                    }
                }
                userReceDetailData.setCouponName(bmsBusinessCoupon.getCouponName());
                userReceDetailData.setExpireTime(getVerRecordDetailsRpVo.getExpireTime());
                userReceDetailData.setCouponType(bmsBusinessCoupon.getCouponType());
            }
        }
        //核销信息
        SearchVerCouponDetailDto.VerRecordData verRecordData = new SearchVerCouponDetailDto.VerRecordData();
        searchVerCouponDetailDto.setVerData(verRecordData);
        verRecordData.setUserName(getVerRecordDetailsRpVo.getUserName());
        verRecordData.setVerName(getVerRecordDetailsRpVo.getVerName());
        verRecordData.setVerNum(getVerRecordDetailsRpVo.getVerNum());
        verRecordData.setVerTime(getVerRecordDetailsRpVo.getVerTime());
        verRecordData.setVerWay(getVerRecordDetailsRpVo.getVerWay());
        //券码
        searchVerCouponDetailDto.getCodes().addAll(JSON.parseArray(getVerRecordDetailsRpVo.getVerCodes(),String.class));
        return RpcResponseVo.success(searchVerCouponDetailDto,ResultMsg.MSG_GET_FAIL);
    }

    @Override
    @PostMapping("/searchTodayYesDayVerCoupon")
    public RpcResponseVo searchTodayYesDayVerCoupon(@RequestBody SearchTodayYesDayVerCouponDto searchTodayYesDayVerCouponDto) {
        Date nowTime = new Date();
        Integer startTime = DateTimeUtils.converDateToInterger(DateTimeUtils.getStartTimeOfDay(DateTimeUtils.addDay(nowTime,-1)));
        Integer endTime = DateTimeUtils.converDateToInterger(DateTimeUtils.getEndTimeOfDay(nowTime));
        ParamsMap paramsMap = new ParamsMap();
        paramsMap.put("businessId",searchTodayYesDayVerCouponDto.getBusinessId());
        paramsMap.put("userIdentify",searchTodayYesDayVerCouponDto.getUserId());
        paramsMap.put("startTime",startTime);
        paramsMap.put("endTime",endTime);
        Map<String,Object> retMap = bmsUserBusinessService.getMyVerRecordByTime(paramsMap);
        if(!ResultMapHelper.isSuccess(retMap)){
            return RpcResponseVo.result(ResultCode.CODE_ERROR_100, ResultMsg.MSG_GET_FAIL);
        }
        List<VerRecordRpDto> verRecordRpDtos = (List<VerRecordRpDto>) retMap.get("data");
        searchTodayYesDayVerCouponDto.getVerRecordRps().addAll(BeanUtils.copyList(verRecordRpDtos,SearchTodayYesDayVerCouponDto.VerRecord.class));
        return RpcResponseVo.success(searchTodayYesDayVerCouponDto,ResultMsg.MSG_GET_FAIL);
    }

    @Override
    public RpcResponseVo verByNo(@RequestBody VerByNoDto verByNoDto) {
        GroupBuyUseReDto groupBuyUseReDto = new GroupBuyUseReDto();
        groupBuyUseReDto.setGroupBuyUseCode(verByNoDto.getCodes());
        groupBuyUseReDto.setVerCodeExtraWay(verByNoDto.getVerWay());
        groupBuyUseReDto.setUserId(verByNoDto.getUserId().toString());
        groupBuyUseReDto.setBusinessId(verByNoDto.getBusinessId());
        Map<String,Object> retMap = bmsDxGroupBuyService.businessUserVerGroupBuyByCode(groupBuyUseReDto);
        if(!ResultMapHelper.isSuccess(retMap)){
            return RpcResponseVo.result(retMap.get("status").toString(),retMap.get("msg").toString());
        }
        return RpcResponseVo.success(null,ResultMsg.HAND_SUCCESS);
    }
}
